package cn.zucc.edu.handyzucc.controller;

import cn.zucc.edu.handyzucc.service.ImageService;
import org.apache.tomcat.util.buf.Utf8Decoder;
import org.apache.tomcat.util.buf.Utf8Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/image")
public class ImageController {
    @Autowired
    private ImageService imageService;

    @RequestMapping(value = "uploadPostImage", method = RequestMethod.POST)
    private Map<String, Object> uploadPostImage(@RequestParam("file") MultipartFile multipartFile) {
        System.out.println(multipartFile.getSize());
        System.out.println(multipartFile.getOriginalFilename());
        Map<String, Object> modelMap = new HashMap<>();
        if (imageService.uploadPostImage(multipartFile)) {
            modelMap.put("code", "0");
        } else {
            modelMap.put("code", "-1");
        }
        return modelMap;
    }

    @RequestMapping(value = "updateAvator", method = RequestMethod.POST)
    private Map<String, Object> updateAvator(@RequestParam("file") MultipartFile multipartFile) {
        System.out.println(multipartFile.getSize());
        System.out.println(multipartFile.getOriginalFilename());
        Map<String, Object> modelMap = new HashMap<>();
        if (imageService.updateAvator(multipartFile)) {
            modelMap.put("code", "0");
        } else {
            modelMap.put("code", "-1");
        }
        return modelMap;
    }

    @RequestMapping(value = "updateNoticeImage", method = RequestMethod.POST)
    private Map<String, Object> updateNoticeImage(@RequestParam("image") MultipartFile multipartFile) {
        System.out.println(multipartFile.getSize());
        System.out.println(multipartFile.getOriginalFilename());
        Map<String, Object> modelMap = new HashMap<>();
        imageService.uploadNoticeImage(multipartFile);
        String url = "https://starock-handyzucc-images.oss-cn-hangzhou.aliyuncs.com/notice/" + multipartFile.getOriginalFilename();
        modelMap.put("code", "0");
        modelMap.put("url", url);
        return modelMap;
    }

    @RequestMapping(value = "updateEventImage", method = RequestMethod.POST)
    private Map<String, Object> updateEventImage(@RequestParam("image") MultipartFile multipartFile) {
        System.out.println(multipartFile.getSize());
        System.out.println(multipartFile.getOriginalFilename());
        Map<String, Object> modelMap = new HashMap<>();
        imageService.uploadEventImage(multipartFile);
        String url = "https://starock-handyzucc-images.oss-cn-hangzhou.aliyuncs.com/event/" + multipartFile.getOriginalFilename();
        modelMap.put("code", "0");
        modelMap.put("url", url);
        return modelMap;
    }

    @RequestMapping(value = "uploadShopImage", method = RequestMethod.POST)
    private Map<String, Object> uploadShopImage(@RequestParam("file") MultipartFile file, @RequestParam("shop_id") Integer id) {
        Map<String, Object> modelMap = new HashMap<>();
        System.out.println(id);
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        imageService.uploadShopImage(file, id);
        modelMap.put("code", "0");
        return modelMap;

    }


}